<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Spring 定时任务与调度深度解析</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        
        .content-card {
            background: rgba(255, 255, 255, 0.95);
            backdrop-filter: blur(10px);
            transition: all 0.3s ease;
        }
        
        .content-card:hover {
            transform: translateY(-2px);
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
        }
        
        .code-block {
            background: #1e1e1e;
            color: #d4d4d4;
            border-radius: 12px;
            overflow: hidden;
            position: relative;
        }
        
        .code-block::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            height: 40px;
            background: #2d2d2d;
            border-bottom: 1px solid #3e3e3e;
        }
        
        .code-block pre {
            margin: 0;
            padding: 60px 24px 24px;
            overflow-x: auto;
        }
        
        .section-title {
            position: relative;
            display: inline-block;
        }
        
        .section-title::after {
            content: '';
            position: absolute;
            bottom: -8px;
            left: 0;
            width: 60px;
            height: 4px;
            background: linear-gradient(90deg, #667eea 0%, #764ba2 100%);
            border-radius: 2px;
        }
        
        .highlight-box {
            background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
            color: white;
            padding: 20px;
            border-radius: 12px;
            margin: 20px 0;
        }
        
        .feature-icon {
            width: 60px;
            height: 60px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-radius: 16px;
            display: flex;
            align-items: center;
            justify-content: center;
            color: white;
            font-size: 24px;
            margin-bottom: 16px;
        }
        
        .timeline-item {
            position: relative;
            padding-left: 40px;
            margin-bottom: 30px;
        }
        
        .timeline-item::before {
            content: '';
            position: absolute;
            left: 10px;
            top: 8px;
            width: 12px;
            height: 12px;
            background: #667eea;
            border-radius: 50%;
        }
        
        .timeline-item::after {
            content: '';
            position: absolute;
            left: 15px;
            top: 20px;
            width: 2px;
            height: calc(100% + 10px);
            background: #e0e0e0;
        }
        
        .timeline-item:last-child::after {
            display: none;
        }
        
        @keyframes fadeInUp {
            from {
                opacity: 0;
                transform: translateY(30px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }
        
        .animate-fadeInUp {
            animation: fadeInUp 0.6s ease-out;
        }
        
        .mermaid {
            background: white;
            padding: 20px;
            border-radius: 12px;
            box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <div class="hero-gradient text-white py-20">
        <div class="container mx-auto px-6">
            <div class="max-w-4xl mx-auto text-center animate-fadeInUp">
                <h1 class="text-5xl font-bold mb-6">Spring 定时任务与调度</h1>
                <p class="text-xl opacity-90 leading-relaxed">深入解析 Spring Framework 中的任务调度机制，掌握企业级应用的定时任务实现</p>
                <div class="mt-8 flex justify-center space-x-4">
                    <span class="bg-white bg-opacity-20 px-4 py-2 rounded-full text-sm">
                        <i class="fas fa-clock mr-2"></i>高效调度
                    </span>
                    <span class="bg-white bg-opacity-20 px-4 py-2 rounded-full text-sm">
                        <i class="fas fa-code mr-2"></i>源码解析
                    </span>
                    <span class="bg-white bg-opacity-20 px-4 py-2 rounded-full text-sm">
                        <i class="fas fa-cogs mr-2"></i>企业实践
                    </span>
                </div>
            </div>
        </div>
    </div>

    <!-- Main Content -->
    <div class="container mx-auto px-6 py-12">
        <!-- Overview Section -->
        <div class="content-card rounded-2xl shadow-xl p-8 mb-8">
            <h2 class="text-3xl font-bold mb-6 section-title">概述</h2>
            <p class="text-lg leading-relaxed text-gray-700 mb-6">
                Spring 提供了强大的定时任务调度功能，允许开发者在应用中轻松配置和管理定时任务。定时任务与调度的实现方式包括基于注解的方式（如 <code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">@Scheduled</code>）、编程方式（如使用 <code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">TaskScheduler</code>）、以及更复杂的调度任务（如 <code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">Quartz</code> 集成）。
            </p>
            
            <!-- Feature Grid -->
            <div class="grid grid-cols-1 md:grid-cols-3 gap-6 mt-8">
                <div class="bg-gray-50 rounded-xl p-6 hover:shadow-lg transition-all">
                    <div class="feature-icon">
                        <i class="fas fa-calendar-check"></i>
                    </div>
                    <h3 class="text-xl font-semibold mb-2">灵活调度</h3>
                    <p class="text-gray-600">支持固定延迟、固定速率和 Cron 表达式等多种调度方式</p>
                </div>
                <div class="bg-gray-50 rounded-xl p-6 hover:shadow-lg transition-all">
                    <div class="feature-icon">
                        <i class="fas fa-layer-group"></i>
                    </div>
                    <h3 class="text-xl font-semibold mb-2">线程池支持</h3>
                    <p class="text-gray-600">基于线程池的异步执行，提高系统吞吐量</p>
                </div>
                <div class="bg-gray-50 rounded-xl p-6 hover:shadow-lg transition-all">
                    <div class="feature-icon">
                        <i class="fas fa-plug"></i>
                    </div>
                    <h3 class="text-xl font-semibold mb-2">易于集成</h3>
                    <p class="text-gray-600">与 Spring 生态系统无缝集成，支持 Quartz 等第三方框架</p>
                </div>
            </div>
        </div>

        <!-- Core Components -->
        <div class="content-card rounded-2xl shadow-xl p-8 mb-8">
            <h2 class="text-3xl font-bold mb-6 section-title">核心组件</h2>
            <p class="text-lg leading-relaxed text-gray-700 mb-6">
                Spring 定时任务的实现主要依赖于 <code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">TaskScheduler</code> 接口和 <code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">@Scheduled</code> 注解。最常用的实现是基于 <code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">ThreadPoolTaskScheduler</code> 的方式。
            </p>

            <!-- Architecture Diagram -->
            <div class="my-8">
                <div class="mermaid">
                    graph TB
                        A[Spring Application] --> B[@EnableScheduling]
                        B --> C[ScheduledAnnotationBeanPostProcessor]
                        C --> D[TaskScheduler]
                        D --> E[ThreadPoolTaskScheduler]
                        C --> F[@Scheduled Methods]
                        F --> G[Fixed Rate Tasks]
                        F --> H[Fixed Delay Tasks]
                        F --> I[Cron Tasks]
                        E --> J[Thread Pool]
                        J --> K[Task Execution]
                </div>
            </div>

            <h3 class="text-2xl font-semibold mt-8 mb-4">TaskScheduler 接口</h3>
            <div class="code-block">
                <pre><code>public interface TaskScheduler {
    ScheduledFuture<?> schedule(Runnable task, Trigger trigger);
    ScheduledFuture<?> scheduleAtFixedRate(Runnable task, long period);
    ScheduledFuture<?> scheduleWithFixedDelay(Runnable task, long delay);
}</code></pre>
            </div>

            <h3 class="text-2xl font-semibold mt-8 mb-4">ThreadPoolTaskScheduler 实现</h3>
            <div class="code-block">
                <pre><code>public class ThreadPoolTaskScheduler extends ExecutorConfigurationSupport implements TaskScheduler, InitializingBean {
    private int poolSize = 10;

    @Override
    public ScheduledFuture<?> schedule(Runnable task, Trigger trigger) {
        return getScheduledExecutor().schedule(new RunnableScheduledFuture(task, trigger));
    }
}</code></pre>
            </div>
        </div>

        <!-- @Scheduled Annotation -->
        <div class="content-card rounded-2xl shadow-xl p-8 mb-8">
            <h2 class="text-3xl font-bold mb-6 section-title">@Scheduled 注解详解</h2>
            
            <div class="highlight-box">
                <h3 class="text-xl font-semibold mb-3"><i class="fas fa-lightbulb mr-2"></i>核心概念</h3>
                <p>@Scheduled 注解是 Spring 中最简单的定时任务实现方式，通过在方法上添加该注解，Spring 会自动安排该方法在指定的时间间隔内定期执行。</p>
            </div>

            <h3 class="text-2xl font-semibold mt-8 mb-4">注解源码</h3>
            <div class="code-block">
                <pre><code>@Target(value=TYPE)
@Retention(value=RUNTIME)
@EnableAspectJAutoProxy(proxyTargetClass=false)
@Import(value=ScheduledAnnotationBeanPostProcessor.class)
public @interface Scheduled {
    String cron() default "";
    long fixedDelay() default -1;
    long fixedRate() default -1;
}</code></pre>
            </div>

            <h3 class="text-2xl font-semibold mt-8 mb-4">执行流程</h3>
            <div class="timeline-item">
                <h4 class="font-semibold text-lg mb-2">定义定时任务</h4>
                <p class="text-gray-600">在方法上使用 @Scheduled 注解来定义定时任务</p>
            </div>
            <div class="timeline-item">
                <h4 class="font-semibold text-lg mb-2">启动应用</h4>
                <p class="text-gray-600">Spring 扫描带有 @Scheduled 注解的任务，通过 ScheduledAnnotationBeanPostProcessor 自动注册</p>
            </div>

            <h3 class="text-2xl font-semibold mt-8 mb-4">使用示例</h3>
            <div class="code-block">
                <pre><code>@Component
public class MyTask {
    @Scheduled(fixedRate = 1000)
    public void task() {
        System.out.println("Executing task at: " + LocalTime.now());
    }
}</code></pre>
            </div>
        </div>

        <!-- Cron Expression -->
        <div class="content-card rounded-2xl shadow-xl p-8 mb-8">
            <h2 class="text-3xl font-bold mb-6 section-title">Cron 表达式</h2>
            <p class="text-lg leading-relaxed text-gray-700 mb-6">
                通过 @Scheduled 注解的 cron 属性，Spring 允许我们使用 Cron 表达式来进行复杂的任务调度。
            </p>

            <div class="bg-gray-100 rounded-xl p-6 mb-6">
                <h3 class="text-xl font-semibold mb-4">Cron 表达式格式</h3>
                <div class="code-block">
                    <pre><code>*    *    *    *    *    *